var fs = require("fs");
var sqlite3 = require('sqlite3');

sqlite3.verbose();

function sqliteDb(file) {
    var self = this;

    var exists = fs.existsSync(file);

    self.file = file;
    self.fileExists = exists;
}

sqliteDb.prototype.createFile = function () {
    var self = this;
    if (!self.fileExists)
        fs.openSync(file, "w");
};

sqliteDb.prototype.connect = function (callback) {
    var self = this;
    self.createFile();
    self.db = new sqlite3.Database(self.file, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, callback);
};

sqliteDb.prototype.open = function () {
    var self = this;
    self.db && self.db.open();
};

sqliteDb.prototype.close = function () {
    var self = this;
    self.db && self.db.close();
};

sqliteDb.prototype.getRow = function (sql, params, callback) {
    var self = this;
    self.db && self.db.get(sql, params, callback);
};

sqliteDb.prototype.executeNonQuery = function (sql, params, callback) {
    var self = this;
    self.db && self.db.run(sql, params, callback);
};

sqliteDb.prototype.executeReader = function (sql, params, callback, complate) {
    var self = this;
    self.db && self.db.each(sql, params, callback, complate);
};

sqliteDb.prototype.getRows = function (sql, params, callback) {
    var self = this;
    self.db && self.db.all(sql, params, callback);
};

module.exports = sqliteDb;